﻿using System;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

/// <summary>
/// Custom validation rule, which compares a context
/// parameter field to 
/// </summary>
namespace WebServicesTesting.Automation.WebTest.ValidationRules
{

    public class CompareContextParameterToDataField : ValidationRule
    {
        private string contextVariableName;
        private string dataFieldValue;

        [Description("Context Variable Name to compare")]
        public string ContextVariableName
        {
            get { return contextVariableName; }
            set { contextVariableName = value; }
        }

        [Description("Data Field Value to compare")]
        public string DataFieldValue
        {
            get { return dataFieldValue; }
            set { dataFieldValue = value; }
        }

        public override string RuleName
        {
            get { return "Comparion of Context Param to Data Field"; }
        }

        public override void Validate(object sender, ValidationEventArgs e)
        {
           
            if (((string)e.WebTest.Context[contextVariableName]).
                Equals(dataFieldValue, StringComparison.InvariantCultureIgnoreCase))
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
                e.Message = string.Format(
                    CultureInfo.CurrentCulture,
                    "The context variables {0} and {1} did not match!",
                    e.WebTest.Context[contextVariableName],
                    DataFieldValue);
            }
            
        }
    }
}